package com.app.bimo.commonbusiness.view.recyclerview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public abstract class RecycleAdapterImpl<T> extends
		RecyclerView.Adapter<RecyclerViewHolder> {
	public abstract int getViewId(int type);

	public abstract RecyclerViewHolder getHold(int id);

	public abstract void initView(RecyclerViewHolder thold, int position);

	protected List<T> list;
	protected Context context;
	public View customHeaderView = null;
	public View customFooterView = null;
	public AdapterView.OnItemClickListener mOnItemClickListener;
	public AdapterView.OnItemLongClickListener mOnItemLongClickListener;


	/**
	 * view的基本类型，这里只有头/底部/普通，在子类中可以扩展
	 */
	public class VIEW_TYPES {

		public static final int HEADER = 7;

		public static final int FOOTER = 8;
	}

	public RecycleAdapterImpl(final List<T> list, Context context) {
		this.list = list;
		this.context = context;
	}

	public T getItem(int position) {
		return list.get(position);
	}

	@Override
	public int getItemCount() {
		// TODO Auto-generated method stub
		int headerOrFooter = 0;
		if (customHeaderView != null) {
			headerOrFooter++;
		}
		if (customFooterView != null) {
			headerOrFooter++;
		}
		return getCount() + headerOrFooter;
	}
	public int getCount(){
		return list.size();
	}
	@Override
	public void onBindViewHolder(final RecyclerViewHolder arg0, int position) {
		// TODO Auto-generated method stub
		if ((customHeaderView != null && position == 0)
				|| (customFooterView != null && position == getItemCount() - 1)) {
			// 如果是header或者是footer则不处理
		} else {
			if (customHeaderView != null) {
				position--;
			}
			final int pos = position;
			// 设置点击事件
			if (mOnItemClickListener != null) {
				arg0.itemView.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						mOnItemClickListener.onItemClick(null, arg0.itemView,
								pos, pos);
					}
				});
			}
			// 设置长按事件
			if (mOnItemLongClickListener != null) {
				arg0.itemView
						.setOnLongClickListener(new View.OnLongClickListener() {
							@Override
							public boolean onLongClick(View view) {
								return mOnItemLongClickListener
										.onItemLongClick(null, arg0.itemView,
												pos, pos);
							}
						});
			}
			arg0.initView();
			initView(arg0, position);
		}

	}

	@Override
	public RecyclerViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
		// TODO Auto-generated method stub
		if (arg1 == VIEW_TYPES.HEADER && customHeaderView != null) {
			return new RecyclerViewHolder(customHeaderView, context);
		} else if (arg1 == VIEW_TYPES.FOOTER && customFooterView != null) {
			return new RecyclerViewHolder(customFooterView, context);
		}
		int id = getViewId(arg1);
		return getHold(id);
	}

	@Override
	public int getItemViewType(int position) {
		if (customFooterView != null && position == getItemCount() - 1) {
			return VIEW_TYPES.FOOTER;
		} else if (customHeaderView != null && position == 0) {
			return VIEW_TYPES.HEADER;
		} else {
			if (customHeaderView != null) {
				return super.getItemViewType(position-1);
			}
			return super.getItemViewType(position);
		}
	}




}
